const dayjs = require('dayjs')

import {
  WorkOrderApi
} from "../../common/apis/workOrder"
import {
  SUB_PAGES
} from "../../config/router.config"
const computedBehavior = require('miniprogram-computed').behavior

import {
  DESIGN_WIDTH,
  getElClientRect,
  getElHeightRpx
} from "../../utils/dom"
import {
  queryParams
} from "../../utils/util"
const app = getApp()
// pagesWorkOrderDetail/pagesWorkOrderDetail.js
Page({
  behaviors: [computedBehavior],
  computed: {

  },
  /**
   * 页面的初始数据
   */
  data: {
    // 工单状态
    orderState: null,
    scrollViewHeight: 0,
    scrollTop: 0,
    // 锚点列表样式
    anchorListStyle: '',
    // 当前锚点位置
    currentAnchor: '',
    // 锚点列表
    anchorList: [],
    // 锚点数据配置
    anchorConfig: {
      evaluate: {
        anchorId: 'evaluate',
        remark: '评价信息',
        label: '评价信息',
        time: '',
        show: false
      },
      receive: {
        anchorId: 'receive',
        remark: '接单信息',
        label: '接单信息',
        time: '',
        show: false
      },
      sendList: {
        anchorId: 'sendList',
        remark: '派单信息',
        label: '派单信息',
        time: '',
        show: false
      },
      info: {
        anchorId: 'info',
        remark: '报修信息',
        label: '报修信息',
        time: '',
        show: false
      },
      fault: {
        anchorId: 'fault',
        remark: '故障信息',
        label: '故障信息',
        time: '',
        show: false
      },
      assistList: {
        anchorId: 'assistList',
        remark: '协作信息(子工单)',
        label: '协作信息(子工单)',
        time: '',
        show: false
      },
      assistMain: {
        anchorId: 'assisMain',
        remark: '协作信息(主工单)',
        label: '协作信息(主工单)',
        time: '',
        show: false
      },

      makeList: {
        anchorId: 'makeList',
        remark: '处理记录',
        label: '处理记录',
        time: '',
        show: false
      },
    },
    showPopver: false,
    orderNum: '',
    detailData: {}
  },
  initStyle() {

    const self = this
    getElHeightRpx('#footer', (height) => {
      console.log(height);
      self.setData({
        scrollViewHeight: app.globalData.viewHeight - height
      })
    })
  },
  initData(orderNum) {
    const self = this
    WorkOrderApi.getDetail(orderNum, (res) => {
      self.setData({
        detailData: res.data
      })
      self.handleAnchorList()
    })
  },
  handleAnchorList() {
    const {
      detailData,
      anchorConfig
    } = this.data
    const names = {
      makeList: '处理信息',
      sendList: '派单信息'
    }
    for (let i in detailData) {
      if (detailData[i] instanceof Array) {

        detailData[i].forEach((element, index) => {
          anchorConfig[`${i}${index}`] = {
            anchorId: `${i}${index}`,
            remark: '处理记录',
            label: `${names[i]}${index}`,
            time: dayjs(element.operDate).format('YYYY-MM-DD HH:MM:ss'),
            show: true
          }
        });
      } else {
        anchorConfig[i].show = !wx.$_.isEmpty(detailData[i])
        if (anchorConfig[i].show) {
          anchorConfig[i].time = dayjs(detailData[i].createTime).format('YYYY-MM-DD HH:MM:ss')
        }
      }
    }
    const resultArray = wx.$_.values(wx.$_.pickBy(anchorConfig, {
      show: true
    }));
    this.setData({
      anchorList: resultArray
    })
  },
  // 返回顶部
  onToTop() {
    this.setData({
      scrollTop: 0
    })
  },
  onAnchorList(e) {
    console.log(e);
    this.setData({
      currentAnchor: e.currentTarget.dataset.item.anchorId
    })
  },

  onAnchor() {
    // console.log('onAnchor', this.data.showPopver);
    this.setData({
      showPopver: !this.data.showPopver
    })
  },
  handleSelected(e) {
    this.setData({
      currentAnchor: e.detail.selected.anchorId
    })
  },
  // 完成工单
  completeWorkOrder() {
    wx.navigateTo({
      url: `${SUB_PAGES.WorkOrder.WORK_ORDER_MY_INDEX_HANDLE.router}${queryParams({
        orderNum:this.data.orderNum,
        assetName: this.data.detailData.fault.equipment,
        assetAddr: this.data.detailData.fault.equipmentAddressId,
        handleState: 'complete'
      })}`,
    })
  },
  // 处理工单
  handleeWorkOrder() {
    wx.navigateTo({
      url: `${SUB_PAGES.WorkOrder.WORK_ORDER_MY_INDEX_HANDLE.router}${queryParams({
        orderNum: this.data.orderNum,
          assetName: this.data.detailData.fault.equipment,
            assetAddr: this.data.detailData.fault.equipmentAddressId,
        handleState: 'handle'
      })}`,
    })
  },
  // 立即评价
  nowEva() {
    wx.navigateTo({
      url: `${SUB_PAGES.WorkOrder.WORK_ORDER_EVA.router}`,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    const {
      orderState,
      orderNum
    } = options
    this.setData({
      orderNum,
      orderState
    })
    this.initData(orderNum)
    this.initStyle()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.popover = this.selectComponent('#popover');
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})